#!/bin/bash
#######################
# @ hoaivubk@gmail.com
# @ have fun
#######################
# START CONFIG DATABASE
HOST="localhost"
DBNAME="test_free"
DBTABLE="freetime"
COL1="SecId"
COL2="TimeDat"
COL3="CallerID"

USER="root"
PASS="123456"
# END CONFIG DATABASE

#verbose="/var/log/asterisk/verbose"
#path="/root/medarbejder"
verbose="./asterisklog.txt"
path="./"

fil101="$path/101.txt"
fil102="$path/102.txt"
fil103="$path/103.txt"
fil104="$path/104.txt"
fil105="$path/105.txt"
fil106="$path/106.txt"
fil203="$path/203.txt"
fil205="$path/205.txt"
fil206="$path/206.txt"
fil207="$path/207.txt"
fil208="$path/208.txt"
fil210="$path/210.txt"

match_id(){
    #echo "match_id __ $1"
    case $1 in
        "101") echo "54"
            ;;
        "102") echo "55"
            ;;
        "103") echo "56"
            ;;
        "104") echo "57"
            ;;
        "105") echo "58"
            ;;
        "106") echo "59"
            ;;
        "203") echo "59"
            ;;
        "205") echo "59"
            ;;
        "206") echo "59"
            ;;
        "207") echo "59"
            ;;
        "208") echo "59"
            ;;
        "210") echo "59"
            ;;
    esac
}

insert_data(){
    entry="$1"
    tmo=${entry:53:3}
    Dat1=`match_id $tmo`
    Dat2=${entry:1:15}
    Dat3=${entry:26:5}
    QRY="mysql -h$HOST -u$USER -p$PASS -D$DBNAME -e \"INSERT INTO $DBTABLE ($COL1, $COL2, $COL3) VALUE ('$Dat1', '$Dat2', '$Dat3');\""
    echo $HOST
    echo $USER
    echo $PASS
    echo $DBNAME
    echo $QRY
    echo $QRY | /bin/sh
}

grep -E "`date +"%b %e"`.*SIP/101.*answered" $verbose > $fil101
while read -r entry; do
    insert_data "$entry"
done < $fil101

grep -E "`date +"%b %e"`.*SIP/102.*answered" $verbose > $fil102
while read -r entry; do
    insert_data "$entry"
done < $fil102

grep -E "`date +"%b %e"`.*SIP/103.*answered" $verbose > $fil103
while read -r entry; do
    insert_data "$entry"
done < $fil103
grep -E "`date +"%b %e"`.*SIP/104.*answered" $verbose > $fil104

while read -r entry; do
    insert_data "$entry"
done < $fil104
grep -E "`date +"%b %e"`.*SIP/105.*answered" $verbose > $fil105
while read -r entry; do
    insert_data "$entry"
done < $fil102

grep -E "`date +"%b %e"`.*SIP/106.*answered" $verbose > $fil106
while read -r entry; do
    insert_data "$entry"
done < $fil106

grep -E "`date +"%b %e"`.*SIP/203.*answered" $verbose > $fil203
while read -r entry; do
    insert_data "$entry"
done < $fil203
grep -E "`date +"%b %e"`.*SIP/205.*answered" $verbose > $fil205
while read -r entry; do
    insert_data "$entry"
done < $fil205
grep -E "`date +"%b %e"`.*SIP/206.*answered" $verbose > $fil206
while read -r entry; do
    insert_data "$entry"
done < $fil206
grep -E "`date +"%b %e"`.*SIP/207.*answered" $verbose > $fil207
while read -r entry; do
    insert_data "$entry"
done < $fil207
grep -E "`date +"%b %e"`.*SIP/208.*answered" $verbose > $fil208
while read -r entry; do
    insert_data "$entry"
done < $fil208
grep -E "`date +"%b %e"`.*SIP/210.*answered" $verbose > $fil210
while read -r entry; do
    insert_data "$entry"
done < $fil210

